/**
 * 
 */
package de.cbf.cam.dsa.facet.talent.edit;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.viewers.ColumnLabelProvider;

import de.cbf.cam.dsa.facet.talent.helper.CompensationTalentReference;
import de.cbf.cam.dsa.facet.talent.helper.editing_support.CompensationTalentEditingSupport;
import de.cbf.cam.dsa.facet.talent.helper.editing_support.CompensationValueEditingSupport;
import de.cbf.util.entity.facet.persistence.resolver.IServiceResolverEntity;
import de.cbf.util.ui.controller.ControllerTableEdit;
import de.cbf.util.ui.helper.ColumnDescription;

/**
 * @author Nebelritter
 *
 */
public class ControllerEditCompensationTalents extends ControllerTableEdit<CompensationTalentReference> {
	private static final Log logger = LogFactory.getLog(ControllerEditCompensationTalents.class);	
	
	public static final String DIALOG_TITLE 		= "Compensation talents..";
	public static final String DIALOG_MESSAGE 		= "Please choose a compensation talent";
	
	protected NumberFormat formatInteger;

	protected IServiceResolverEntity talentResolver;
	
	public ControllerEditCompensationTalents() {		
		super(CompensationTalentReference.class);
		formatInteger = NumberFormat.getIntegerInstance();
	}
	
	protected List<ColumnDescription> getColumnDescriptions() {
		List<ColumnDescription> descriptions = new ArrayList<ColumnDescription>();
		descriptions.add(provideColumnDescriptionTalentUUID());
		descriptions.add(provideColumnDescriptionTalentName());
		descriptions.add(provideColumnDescriptionCompensationValue());		
		return descriptions;
	}
	
	protected ColumnDescription provideColumnDescriptionTalentUUID(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setEditable(false);
		columnDescription.setTitle("UUID");
		if(System.getProperty("isTest", "false").equals("true")){
			columnDescription.setWidth(100);
		}else{
			columnDescription.setWidth(0); //uuid should not be seen in production
		}		
		columnDescription.setLabelProvider(new ColumnLabelProvider(){
			public String getText(Object element) {
				if (element instanceof CompensationTalentReference) {
					CompensationTalentReference compensationTalent = (CompensationTalentReference)element;
					UUID savedUUID = compensationTalent.getSavedUuid();
					if(savedUUID != null){
						return savedUUID.toString();
					}else{
						return "";
					}					
				}else{
					String err_msg = "entity of wrong type in getText(TalentUUID) : \n"+
							"Expected: "+CompensationTalentReference.class.getSimpleName()+"\n"+ 
							"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
					logger.error(err_msg);
				}
				return "error";
			}
		});
		return columnDescription;
	}
	
	protected ColumnDescription provideColumnDescriptionTalentName(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setEditable(true);
		columnDescription.setTitle("Name");
		columnDescription.setWidth(200);
		columnDescription.setLabelProvider(new ColumnLabelProvider(){
			public String getText(Object element) {
				if (element instanceof CompensationTalentReference) {
					CompensationTalentReference compensationTalent = (CompensationTalentReference)element;
					return compensationTalent.getSavedName();
				}else{
					String err_msg = "entity of wrong type in getText(TalentName) : \n"+
							"Expected: "+CompensationTalentReference.class.getSimpleName()+"\n"+ 
							"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
					logger.error(err_msg);
				}
				return "error";
			}
		});
		columnDescription.setEditSupportClass(CompensationTalentEditingSupport.class);
		columnDescription.setEditingSupportCustomizeObject(talentResolver);
		return columnDescription;
	}
	
	protected ColumnDescription provideColumnDescriptionCompensationValue(){
		ColumnDescription columnDescription = new ColumnDescription();
		columnDescription.setEditable(true);
		columnDescription.setTitle("Value");
		columnDescription.setWidth(60);
		columnDescription.setLabelProvider(new ColumnLabelProvider(){
			public String getText(Object element) {
				if (element instanceof CompensationTalentReference) {
					CompensationTalentReference compensationTalent = (CompensationTalentReference)element;
					Integer compensationValue = compensationTalent.getCompensationValue();
					if(compensationValue != null){
						String valueString = formatInteger.format(compensationValue);
						if(compensationValue > 0){
							valueString = "+"+valueString;
						}
						return valueString;
					}else{
						return "";
					}					
				}else{
					String err_msg = "entity of wrong type in getText(CompensationValue) : \n"+
							"Expected: "+CompensationTalentReference.class.getSimpleName()+"\n"+ 
							"Got: "+ (element == null ? "null" : element.getClass().getSimpleName());
					logger.error(err_msg);
				}
				return "error";
			}
		});
		columnDescription.setEditSupportClass(CompensationValueEditingSupport.class);
		return columnDescription;
	}

	public void setTalentResolver(IServiceResolverEntity serviceResolverTalents) {
		this.talentResolver = serviceResolverTalents;		
	}
}
